<?php

define('ACC', true);

require('../includes/init.php');

if(!isset($_SESSION['auth_list'])){
	$smarty->display('./admin/login.html');
}
$arr = explode(",", $_SESSION['auth_list']);
if(in_array('admin_manage', $arr) == 0){
	$smarty->assign('message', '您没有权限进行此操作！');
	$smarty->assign('title', '系统提示');
	$smarty->assign('url', 'javascript:history.go(-1);');
	$smarty->assign('link', '返回');
	$smarty->display('./admin/error.html');
	exit;
}

$manager = new ManagerModel();

if($_GET['act'] == 'list'){
	
	$manager_list = $manager->get_manager_list();
	
	foreach($manager_list as $key=>$val){
		$manager_list[$key]['add_time'] = date('Y-m-d h:i:s', $val['add_time']);
		$manager_list[$key]['last_login'] = date('Y-m-d h:i:s', $val['last_login']);
	}
	$smarty->assign('title', '管理员列表');
	$smarty->assign('url', 'manager.php?act=add');
	$smarty->assign('link', '添加管理员');
	$smarty->assign('manager_list', $manager_list);
	
	$smarty->display('./admin/manager_list.html');
	
}else if($_GET['act'] == 'add'){
	
	$smarty->assign('title', '添加管理员');
	$smarty->assign('url', 'manager.php?act=list');
	$smarty->assign('link', '管理员列表');
	$smarty->assign('action', 'insert');
	$smarty->assign('role_list', $manager->get_role_list());
	
	$smarty->display('./admin/manager_info.html');
	
}else if($_GET['act'] == 'insert'){
	
	$data['username'] = trim($_POST['username']);
	$data['email'] = trim($_POST['email']);
	$data['password'] = md5(trim($_POST['password']));
	$data['role_id'] = intval($_POST['select_role']);
	$data['add_time'] = time();
	
	if($manager->insert_admin_user($data)){
		$desc = '添加'.$data['username'].'成功';
		$url = 'manager.php?act=list';
		$link = '管理员列表';
		admin_msg($smarty, $desc, $url, $link);
	}
	
}else if($_GET['act'] == 'delete'){
	
	$user_id = $_GET['id'];
	if($manager->delete_admin_user($user_id)){
		echo '1';
	}else{
		echo '0';
	}
	
}else if($_GET['act'] == 'edit'){
	
	$user = $manager->select_user_info($_GET['id']);
	$role_list = $manager->get_role_list();

	$smarty->assign('title', '编辑管理员');
	$smarty->assign('url', 'manager.php?act=list');
	$smarty->assign('link', '管理员列表');
	$smarty->assign('action', 'update');
	$smarty->assign('user', $user);
	$smarty->assign('role_list', $role_list);
	
	$smarty->display('./admin/manager_info.html');
	
}else if($_GET['act'] == 'update'){
	
	$user_id = intval($_POST['id']);
	$username = trim($_POST['username']);
	$email = trim($_POST['email']);
	$password = md5(trim($_POST['password']));
	$role_id = intval($_POST['select_role']);
	
	if($user_id == 0 || $role_id == 0){
		echo '信息错误!';
		exit;
	}
	$data['user_id'] = $user_id;
	if($username != ''){
		$data['username'] = $username;
	}
	if($email != ''){
		$data['email'] = $email;
	}
	if($password != ''){
		$data['password'] = $password;
	}
	$data['role_id'] = $role_id;

	if($manager->update_user_info($data)){
		$desc = '修改' . $username . '成功';
		$url = 'manager.php?act=list';
		$link = '管理员列表';
		admin_msg($smarty, $desc, $url, $link);
	}
	
}




?>